﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

using Benefit.Models.Sys;
namespace Benefit.Service.Day
{
    /// <summary>
    /// 盈利或亏损所占的Tick
    /// </summary>
    public class ServiceDayWinOrLoseTick : Benefit.Interface.Day.IDayWinOrLoseTick
    {
        Benefit.DB.DBManager db = null;
        public ServiceDayWinOrLoseTick(Benefit.DB.DBManager db)
        {
            this.db = db;
        }

        /// <summary>
        /// 得出超出止损次数
        /// </summary>
        /// <param name="pdate"></param>
        /// <param name="serverAccountId"></param>
        /// <param name="stopLoseTick"></param>
        /// <returns></returns>
        public int GetOverStopLoss(string pdate, int serverAccountId, int stopLoseTick)
        {
            int count = 0;
            var query = from t in db.DayWinOrLoseTick where t.Win == false && t.Tick >= stopLoseTick select t;//zhege?
            if (query.Count() > 0)
            {
                count = query.Sum(a => a.Count);
            }
            return count;
        }

        /// <summary>
        /// 得出超出止损次数
        /// </summary>
        /// <param name="pdate"></param>
        /// <param name="serverAccountId"></param>
        /// <param name="stopLoseTick"></param>
        /// <returns></returns>
        public int GetInstrumentOverStopLoss(string pdate, int serverAccountId, int stopLoseTick, int instrumentid, int direction)
        {
            int count = 0;
            var query = from t in db.DayWinOrLoseTick where t.Win == false && t.Tick >= stopLoseTick && t.InstrumentId == instrumentid && t.Direction == direction select t;//zhege?
            if (query.Count() > 0)
            {
                count = query.Sum(a => a.Count);
            }
            return count;
        }

        /// <summary>
        /// 得出超出止损次数
        /// </summary>
        /// <param name="pdate"></param>
        /// <param name="serverAccountId"></param>
        /// <param name="stopLoseTick"></param>
        /// <returns></returns>
        public int GetProductOverStopLoss(string pdate, int serverAccountId, int stopLoseTick, int productid, int direction)
        {
            int count = 0;
            var query = from t in db.DayWinOrLoseTick where t.Win == false && t.Tick >= stopLoseTick && t.Instrument.ProductId == productid && t.Direction == direction select t;//zhege?
            if (query.Count() > 0)
            {
                count = query.Sum(a => a.Count);
            }
            return count;
        }

        /// <summary>
        /// 得出品种胜率
        /// </summary>
        /// <param name="pdate"></param>
        /// <param name="serverAccountId"></param>
        /// <param name="stopLoseTick"></param>
        /// <returns></returns>
        public double GetProductWinRate(string pdate, int serverAccountId, int productid, int direction)
        {
            int Wincount = 0;
            int Lostcount = 1;
            var query = from t in db.DayWinOrLoseTick where t.Win == false && t.Win == false && t.Instrument.ProductId == productid && t.Direction == direction select t;
            if (query.Count() > 0)
            {
                Wincount = query.Count();
            }

            var query2 = from t in db.DayWinOrLoseTick where t.Win == false && t.Win == true && t.Instrument.ProductId == productid && t.Direction == direction select t;
            if (query2.Count() > 0)
            {
                Lostcount = query2.Count();
            }
            return Wincount / Lostcount;
        }


        /// <summary>
        /// 初始化Tick值
        /// </summary>
        /// <param name="pdate"></param>
        /// <param name="tradeHistoryId"></param>
        public void InitTickValue(string pdate, int tradeHistoryId)
        {
            int dayTickTableCount1 = db.DayWinOrLoseTick.Count();

            var query = (from t in db.ClosePositionDetail where t.TradingDay.Equals(pdate) select t).ToList();
            Interface.ServerData.IT_Instrument iinstru = new ServerData.ServiceT_Instrument(db);
            Interface.ServerData.IT_Account ita = new ServerData.ServiceT_Account(db);
            Interface.Sys.IInstrument iin = new Sys.ServiceInstrument(db);
            Interface.Sys.IAccount iacount = new Sys.ServiceAccount(db);
            List<Models.ServerData.T_Instrument> instruments = iinstru.GetList();
            List<Models.ServerData.T_Account> serverAccounts = ita.GetList();
            foreach (Models.ServerData.T_ClosePositionDetail_History h in query)
            {
                double winPrice = h.TradePrice - h.OpenPrice;
                var _query = from t in instruments where t.InstrumentID.Equals(h.InstrumentId) select t;
                if (_query.Count() > 0)
                {
                    double tick = _query.First().PriceTick;
                    int allTick = Convert.ToInt32(winPrice / tick);
                    Models.Day.DayWinOrLoseTick dayTick = new Models.Day.DayWinOrLoseTick();
                    if (winPrice > 0)
                    {
                        dayTick.Win = true;
                    }
                    else
                    {
                        dayTick.Win = false;
                    }
                    dayTick.TradeHistoryId = tradeHistoryId;
                    dayTick.Tick = Math.Abs(allTick);
      
                    dayTick.InstrumentId = iin.GetInstrumentId(h.InstrumentId);
                    dayTick.Direction = Convert.ToInt32(h.Direction);
                    int loginAccountId = h.AccountId;
                    var getAccount = from t in serverAccounts where t.Id == loginAccountId select t;
         
                    //   Sys.Account account = new Sys.Account();
                    dayTick.AccountId = iacount.GetAccountId(getAccount.First().LoginAccount);
                    var countTick = from t in db.DayWinOrLoseTick where t.AccountId == dayTick.AccountId && t.Tick == dayTick.Tick && t.TradeHistoryId == dayTick.TradeHistoryId && t.Win == dayTick.Win && t.InstrumentId == dayTick.InstrumentId && t.Direction == dayTick.Direction select t;
                    if (countTick.Count() > 0)
                    {
                        countTick.First().Count++;
                        db.SaveChanges();
                    }
                    else
                    {
                        dayTick.Count = 1;
                        db.DayWinOrLoseTick.Add(dayTick);
                        db.SaveChanges();
                    }
                }
            }
        }
        /// <summary>
        /// 删除某天所有信息
        /// </summary>
        /// <param name="tradeHistoryId"></param>
        public void Delete(int tradeHistoryId)
        {
            var query = from t in db.DayWinOrLoseTick where t.TradeHistoryId == tradeHistoryId select t;
            foreach (Models.Day.DayWinOrLoseTick t in query)
            {
                db.DayWinOrLoseTick.Remove(t);
            }
            db.SaveChanges();
        }
    }
}